package ogs.lib.mina.core.filterchain
{
	public class IoFilterChainNodeImpl implements IoFilterChainNode
	{
		private var filterChain:IoFilterChain
		
		internal var prevNode:IoFilterChainNodeImpl;
		
		internal var nextNode:IoFilterChainNodeImpl;
		
		private var name:String;
		
		private var filter:IoFilter;
		
		private var nextFilter:NextFilter;
		
		public function IoFilterChainNodeImpl(filterChain:DefaultIoFilterChain,
											  prevNode:IoFilterChainNodeImpl, 
											  nextNode:IoFilterChainNodeImpl,
											  name:String, filter:IoFilter)
		{
			if (filter == null)
			{
				throw new ArgumentError("filter");
			}
			
			if (name == null)
			{
				throw new ArgumentError("name");
			}
			
			this.filterChain = filterChain;
			this.prevNode = prevNode;
			this.nextNode = nextNode;
			this.name = name;
			this.filter = filter;
			this.nextFilter = new NextFilterImpl(filterChain, this);
		}
		
		public function getName():String
		{
			return name;
		}
		
		public function getFilter():IoFilter
		{
			return filter;
		}
		
		internal function setFilter(filter:IoFilter):void
		{
			if (filter == null) {
				throw new ArgumentError("filter");
			}
			
			this.filter = filter;
		}

		public function getNextFilter():NextFilter
		{
			return nextFilter;
		}
		
		public function addBefore(name:String, filter:IoFilter):void
		{
			filterChain.addBefore(getName(), name, filter);
		}
		
		public function addAfter(name:String, filter:IoFilter):void
		{
			filterChain.addAfter(getName(), name, filter);
		}
		
		public function replace(newFilter:IoFilter):void	
		{
			filterChain.replaceByName(getName(), newFilter);
		}
		
		public function remove():void
		{
			filterChain.removeByName(getName());
		}
	}
}